package org.qs.webservice.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;

import org.qs.webservice.exception.QSReadException;
import org.qs.webservice.service.BookService;
import org.qs.webservice.util.IReadUtils;
import org.springframework.stereotype.Component;


@Component
@Path("/v{version}/content")
public class ChapterContentRestController {
	
	@Resource
	private BookService bookService;
	/**
	 * 获取书籍详情
	 * @param book_id
	 * @return
	 */
	@GET
    @Path("/{book_id}/{chapter_id}/index")
	public String getInfo(@PathParam("book_id")int book_id,@PathParam("chapter_id")int chapter_id,@HeaderParam("token")String token,@HeaderParam("Info-Uuid")String device_identifier){
		try {
			String ret = bookService.updateChapterContent(book_id,chapter_id,token,device_identifier);
			return ret;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("chapter_id", chapter_id);
			QSReadException.printf(e, "获取章节内容异常",errorJson);
			Map<String,Object> tokenMap =  new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	/**
	 * 获取书籍详情
	 * @param book_id
	 * @return
	 */
	@GET
    @Path("/index")
	public String getContent(@QueryParam("book_id")int book_id,@QueryParam("chapter_id")int chapter_id,@HeaderParam("token")String token,@HeaderParam("Info-Uuid")String device_identifier){
		try {
			String ret = bookService.updateChapterContent(book_id,chapter_id,token,device_identifier);
			return ret;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("chapter_id", chapter_id);
			QSReadException.printf(e, "获取章节内容异常",errorJson);
			Map<String,Object> tokenMap =  new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
}
